﻿using System;
using System.Data;
using TTS;
using TTSBLL;
using TTSModel;

public partial class T0002000_Add : System.Web.UI.Page
{
    private Assemble a;
    private UnitType u;
    private string message;
    private string T0002040_SN { get { return ViewState["T0002040_SN"].ToString(); } set { ViewState["T0002040_SN"] = value; } }
    private string T0002000_SN { get { return ViewState["T0002000_SN"].ToString(); } set { ViewState["T0002000_SN"] = value; } }
    private DataRow dr;
    private DataTable dt
    {
        get
        {
            return (DataTable)ViewState["dt"];
        }
        set
        {
            ViewState["dt"] = value;
        }
    }

    public T0002000_Add()
    {
        a = new Assemble();
        u = new UnitType();
    }

    /// <summary>
    /// 首次加载页面的时候定义相应datatable结构
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            dt = new DataTable();
            CommonHelper.AssembleItemDataTableInitialize(dt);
            this.Label1.Text = u.UnitType_GetbyTcode("T0002040").TcodeVersion;
            this.Label2.Text = u.UnitType_GetbyTcode("T0002000").TcodeVersion;
        }
        this.TextBox1.Focus();
    }

    /// <summary>
    /// 针对第一个SN的检验
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
        T0002040_SN = this.TextBox1.Text.Trim();
        if (SNHelper.SNCheck(T0002040_SN, "T0002040", out message))
        {
            if (SNHelper.SNCheckVersion(T0002040_SN, this.Label1.Text, out message))
            {
                if (ProConditionHelper.CheckMainSNsPrecondition(T0002040_SN, "T0002040", out message))
                {
                    this.TextBox1.Enabled = false;
                    this.TextBox2.Enabled = true;
                    this.TextBox2.Focus();
                    return;
                }
            }           
        }
        ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + message + "');</script>");
        this.TextBox1.Text = "";
    }

    /// <summary>
    /// 针对第二个SN的检验
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void TextBox2_TextChanged(object sender, EventArgs e)
    {
        T0002000_SN = this.TextBox2.Text.Trim();
        if (SNHelper.SNCheck(T0002000_SN, "T0002000", out message))
        {
            if (SNHelper.SNCheckVersion(T0002000_SN, this.Label2.Text, out message))
            {
                if (ProConditionHelper.CheckLeafSubSNsPrecondition(T0002000_SN, "T0002000", out message))
                {
                    this.TextBox2.Enabled = false;
                    this.Button1.Enabled = true;
                    this.Button1.Focus();
                    return;
                }
            }          
        }
        ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + message + "');</script>");
        this.TextBox2.Text = "";
        this.TextBox2.Focus();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Unit u = new Unit();
        u.Insert(new UnitInfo() { SN = T0002040_SN, Tcode = "T0002040", IsLeaf = true, IsRoot = true });
        u.Insert(new UnitInfo() { SN = T0002000_SN, Tcode = "T0002000", IsLeaf = true, IsRoot = true });

        //后续使用Userid=User.Identity.Name
        AssembleInfo ainfo = new AssembleInfo()
        {
            ID = 0,
            SN = T0002040_SN,
            IsRoot = true,            
            RecordTime = DateTime.Now,
            Tcode = "T0002040",
            Userid = "111111"
        };

        dt.Rows.Clear();
        dr = dt.NewRow();
        dr["SubSN"] = T0002000_SN;
        dr["Remark"] = "RRH_digitalmoudle_antilightingboard";
        dt.Rows.Add(dr);
        if (ProConditionHelper.CheckMainSNsPrecondition(T0002040_SN, "T0002040", out message))
        {
            if (ProConditionHelper.CheckLeafSubSNsPrecondition(T0002000_SN, "T0002000", out message))
            {
                if (a.Assemble_Insert(ainfo, dt))
                {
                    message = "录入成功";
                    this.TextBox1.Text = "";
                    this.TextBox2.Text = "";
                    this.TextBox1.Enabled = true;
                    this.TextBox1.Focus();
                }
                else
                {
                    message = "录入失败，请联系相关人员";
                }
            }
        }
        
        ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + message + "');</script>");
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("RRH.aspx");
    }
}
